<template>
	<div class="widgets bg-white">
		<div class="btn-group right">
			<el-button type="info">刷新</el-button>
			<el-button type="primary" v-if="!isAdd[0]" disabled>新增</el-button>
			<el-button type="primary" v-else @click="add">新增</el-button>
			<el-button type="success" v-if="status[0] === '5' && reviewerTableData.length === 0" disabled>保存</el-button>
			<el-button type="success" v-if="status[0] === '5' && reviewerTableData.length > 0" @click="save">保存</el-button>
			<el-button type="danger" v-if="multipleSelection.length <= 0" disabled>删除</el-button>
			<el-button type="danger" v-else @click="del">删除</el-button>
		</div>
	</div>
</template>
<script>
export default {
	data() {
		return {
			status: this.$store.state.verifyProcessStore.status,
			isAdd: this.$store.state.verifyProcessStore.isAdd,
			reviewerTableData: this.$store.state.verifyProcessStore.reviewerTableData,
			multipleSelection: this.$store.state.verifyProcessStore.multipleSelection
		}
	},
	methods: {
		add() {		//新增
			switch(this.status[0]) {
				case '1':
					this.$util.openLayerIframe({title: '新增-会计科目', zIndex: 13, width: '600px', height: '450px', url: this.$domain + '/#/basicData/verifyProcess/oneAdd'});
					break;
				case '2':
					this.$util.openLayerIframe({title: '新增-部门', zIndex: 13, width: '600px', height: '450px', url: this.$domain + '/#/basicData/verifyProcess/twoAdd'});
					break;
				case '3':
					this.$util.openLayerIframe({title: '新增-职员', zIndex: 13, width: '600px', height: '450px', url: this.$domain + '/#/basicData/verifyProcess/threeAdd'});
					break;
				case '4':
					this.$util.openLayerIframe({title: '新增-金额', zIndex: 13, width: '450px', height: '240px', url: this.$domain + '/#/basicData/verifyProcess/fourAdd'});
					break;
				case '5':
					this.$util.openLayerIframe({title: '新增-审核人', zIndex: 13, width: '550px', height: '480px', url: this.$domain + '/#/basicData/verifyProcess/fiveAdd'});
					break;
				default: break;
			}
		},
		save() {	//审批流程保存
			let store = this.$store.state.verifyProcessStore,
				tableData = store.reviewerTableData,		//审核人表格数据
				nameArray = ['name', 'name1', 'name2', 'name3'],	//要校验的字段
				approversArray = [],
				notifyPeoplesArray = [],
				copyPeoplesArray = [],
				approvers = [],
				notifyPeoples = [],
				copyPeoples = []
			tableData.forEach((item, index) => {
				if(item['nodeType'] === 0) {					//筛选审核人
					item['rowNumber'] = Number(index + 1);		//标识行号
					approversArray.push(item);
				}
				if(item['nodeType'] === 1) {					//筛选知会人
					item['rowNumber'] = Number(index + 1);		//标识行号
					notifyPeoplesArray.push(item);
				}
				if(item['nodeType'] === 2) {					//筛选
					item['rowNumber'] = Number(index + 1);		//标识行号
					copyPeoplesArray.push(item);
				}
			});

			approvers = this.filterData(approversArray);
			notifyPeoples = this.filterData(notifyPeoplesArray);
			copyPeoples = this.filterData(copyPeoplesArray);

			let data = {
				bills: [String(store.billId)],					//单据id
				verchars: [String(store.vercharId)],			//会计科目id
				departments: [String(store.deptId)],			//部门id
				employees: [String(store.empId)],				//职员id
				amounts: store.amounts,							//分段金额
				approvers: approvers,							//审核人id
				notifyPeoples: notifyPeoples,					//知会人id
				copyPeoples: copyPeoples						//抄送人id
			}

			//保存数据
			this.$http.post('activiti/insertMain', JSON.stringify(data), {
				headers: {
					'Content-Type': 'application/json'
				}
			}).then(res => {
				if(res.data.code == 20001) {
					this.$message({message: res.data.data || '保存成功', duration: 1500, type: 'success'});
				} else {
					this.$message({message: res.data.msg || '保存失败', duration: 1500, type: 'error'});
				}
			})
		},
		filterData(array) {
			/*
				拼接 审核人，知会人，抄送人节点数据
				格式：["金额id, (审核人 | 知会人 | 抄送人)id, 同类型节点下的索引, 是否生成单据, 所在表格的序号"]
			*/
			let newArr = [],
				nameArray = ['name', 'name1', 'name2', 'name3'],
				store = this.$store.state.verifyProcessStore;
			if(array.length > 0) {
				array.forEach((item, index) => {
					nameArray.forEach(nameItem => {
						if(item[nameItem]) {
							newArr.push(store.moneyId + ',' + item[nameItem] + ',' + (index + 1) + ',' + item['generateDocument'] + ',' + item['rowNumber']);
						}
					});
				});
			}
			return newArr;
		},
		del() {
			this.$confirm('确定删除选中数据?', '删除提示', {
	          confirmButtonText: '确定',
	          cancelButtonText: '取消',
	          type: 'warning'
	        }).then(() => {
	        	this.$message({message: '删除成功', type: 'success', duration: 1000});
	        	setTimeout(() => {
	        		let treeData = this.$store.state.verifyProcessStore.treeData;
	        		this.$store.commit('verifyProcessStore/deleteNode', {arr: this.multipleSelection, treeData: treeData});
	        	}, 1000);
	        }).catch(() => {
	        	this.$message({message: '已取消删除', type: 'info', duration: 1000});
	        });
		}
	}
}
</script>